<?php
namespace Admin\Controller;

class AdminController extends CommonController{
	public function add(){
		if(IS_GET){
			$Model = D("Role");
			$data = $Model->select();
			$this->assign("data",$data);
			$this->display();
		}else{
			$Model = D("Admin");
			$data = $Model->create();
			if(!$data){
				$this->error($Model->getError());
			}
			$Model->add($data);
			$this->success("添加成功");
		}
	}
	public function index(){
		//显示用户账号
		$Model = D("Admin");
		$data = $Model->listData();
		if(!$data){
			$this->error($Model->getError());
		}
		$this->assign("data",$data);
		$this->display();
	}
	public function dels(){
		$id = intval(I("get.id"));
		if($id <= 1){
			$this->error("参数错误");
		}
		$Model = D("Admin");
		$res = $Model->dels($id);
		if($res === false){
			$this->error("删除错误");
		}
		$this->success("删除成功");
	}
	public function edit(){
		$Model = D("Admin");
		if(IS_GET){	
			$id = intval(I("get.id"));
			if($id <= 1){
				$this->error("参数错误");
			}
			//获取用户信息
			$data = $Model->findOne($id);
			if($data === false){
				$this->error("参数错误");
			}
			//获取角色信息
			$role = D("Role")->select();
			$this->assign("role",$role);
			$this->assign("data",$data);
			$this->display();
		}else{
			//修改用户信息
		    $data = $Model->create();
		    if(!$data){
		    	$this->error($Model->getError());
		    }
		    $res = $Model->update($data);
		    if($res === false){
		    	$this->error("修改失败");
		    }
		    $this->success("修改成功");
		}
	}
}